package com.ht.modules.system.online.codegenerate.generate.a;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Data
public class LoadTemplatesUtil {
    private String templatePath;
    private List<File> templateRootDirs = new ArrayList<>();
    private String stylePath;
    private String vueStyle;

    public List<File> loadTemplates() {
        URL url = this.getClass().getResource(this.templatePath);
        if (url == null) {
            throw new RuntimeException(" >> 模板加载失败，请重新编译jeecg-system-biz项目，templatePath = " + this.templatePath);
        }
        String urlFile = url.getFile();
        urlFile = URLDecoder.decode(urlFile, StandardCharsets.UTF_8);
        urlFile = urlFile.replaceAll("%20", " ");
        if (urlFile.contains("/BOOT-INF/classes!") || urlFile.contains("/BOOT-INF/lib/") || urlFile.contains(".jar!")) {
            urlFile = System.getProperty("user.dir") + File.separator + "config/jeecg/code-template-online/".replace("/", File.separator);
        }
        File file = new File(urlFile);
        if (file.exists()) {
            this.setTemplateRootDirs(Collections.singletonList(file));
        }
        return this.templateRootDirs;
    }
}
